home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP0492.ARJ / CHECKEXE.C < prev    next >
C/C++ Source or Header  |  1990-09-05  |  3KB  |  126 lines

  1. /*
  2. ** CHECKEXE.C - checksum protection for executable files
  3. **
  4. ** by: Bob Jarvis
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <stdlib.h>
  11.  
  12. static struct {
  13.       unsigned char marker[16];
  14.       unsigned long checksum;
  15. } marker_struct = {"CHECKEXE MARKER",0L};
  16.  
  17. void checkexe(char *fname)
  18. {
  19.       FILE *fptr;
  20.       unsigned int c;
  21.       int first_time = 0, i;
  22.       char buffer[14];
  23.       unsigned long chksum = 0L;
  24.       unsigned long l;
  25.       unsigned long marker_offset;
  26.       unsigned char *charptr;
  27.       time_t tm;
  28.  
  29.       fptr = fopen(fname,"r+b");
  30.       if(fptr == NULL)
  31.       {
  32.             fprintf(stderr,"checkexe : unable to open input file '%s'\n",
  33.                   fname);
  34.             exit(99);
  35.       }
  36.  
  37.       setvbuf(fptr, NULL, _IOFBF, 32767);    /* try to get a 32K buffer */
  38.  
  39.    /* 
  40.     * If this is the first time the check has been run, scan the entire file
  41.     * to find the marker.  Otherwise proceed.
  42.     */
  43.  
  44.       if(marker_struct.checksum == 0L)
  45.       {
  46.             first_time = 1;
  47.  
  48.             c = fgetc(fptr);
  49.             while(!feof(fptr))
  50.             {
  51.                   if(c == (unsigned int)marker_struct.marker[0])
  52.                   {
  53.                         fread(buffer,sizeof(buffer),1,fptr);
  54.                         if(memcmp(buffer,&marker_struct.marker[1],
  55.                               sizeof(buffer)) == 0)
  56.                         {
  57.                               marker_offset = ftell(fptr) + 1L;
  58.                               break;
  59.                         }
  60.                         fseek(fptr,-13L,SEEK_CUR);
  61.                   }
  62.  
  63.                   c = fgetc(fptr);
  64.             }
  65.  
  66.             if(feof(fptr))
  67.             {
  68.                   fprintf(stderr,"checkexe : unable to locate marker\n");
  69.                   exit(99);
  70.             }
  71.  
  72.       /* Change the marker field to random values */
  73.  
  74.             tm = time(NULL);
  75.  
  76.             srand((unsigned int)tm);
  77.  
  78.             for(i = 0 ; i < sizeof(marker_struct.marker) ; ++i)
  79.                   marker_struct.marker[i] = (unsigned char) rand();
  80.  
  81.             fseek(fptr,marker_offset - sizeof(marker_struct.marker),SEEK_SET);
  82.  
  83.             fwrite(marker_struct.marker,sizeof(marker_struct.marker),1,fptr);
  84.       }
  85.  
  86.    /* Calculate the checksum for the entire file */
  87.  
  88.       rewind(fptr);
  89.  
  90.       c = fgetc(fptr);
  91.       for(l = 0 ; !feof(fptr) ; ++l)
  92.       {
  93.             chksum += (unsigned long)c;
  94.             c = fgetc(fptr);
  95.       }
  96.  
  97.       if(first_time)
  98.       {
  99.             marker_struct.checksum = chksum;
  100.             fseek(fptr,marker_offset,SEEK_SET);
  101.             fwrite(&marker_struct.checksum,sizeof(unsigned long),1,fptr);
  102.       }
  103.       else
  104.       {
  105.             charptr = (unsigned char*) &marker_struct.checksum;
  106.  
  107.             for(i = 0 ; i < sizeof(marker_struct.checksum) ; ++i)
  108.                   chksum -= (unsigned long)(charptr[i]);
  109.  
  110.             if(chksum != marker_struct.checksum)
  111.             {
  112.                   fprintf(stderr, "\acheckexe : %s has been altered, "
  113.                         "possibly by a virus\n", fname);
  114.                   exit(99);
  115.             }
  116.       }
  117.  
  118.       fclose(fptr);
  119.       return;
  120. }
  121.  
  122. main(int argc, char *argv[])
  123. {
  124.       checkexe(argv[0]);
  125. }
  126.